package com.ylkj.patient.utils;

import android.content.Context;
import android.text.TextUtils;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.facebook.react.bridge.Promise;
import com.google.gson.Gson;
import com.yilijk.base.utils.ALog;
import com.yilijk.base.utils.SharedPreferencesUtils;
import com.ylkj.patient.MainApplication;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;

import java.util.HashMap;

public class AMapLocationUtils {

    public static void getLocalInfo(@NotNull Promise promise) {
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
        mLocationOption.setOnceLocation(true);
        mLocationOption.setOnceLocationLatest(true);
        //初始化定位
        AMapLocationClient mLocationClient = new AMapLocationClient(MainApplication.getContext());
        AMapLocationListener mAMapLocationListener = new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation amapLocation) {
                if (amapLocation != null) {
                    if (amapLocation.getErrorCode() == 0) {
//可在其中解析amapLocation获取相应内容。
                        amapLocation.getLatitude();//获取纬度
                        amapLocation.getLongitude();//获取经度
                        amapLocation.getCity();//城市信息
                        JSONArray newArray = new JSONArray();
                        newArray.put(amapLocation.getCity());
                        newArray.put(amapLocation.getLatitude() + "");
                        newArray.put(amapLocation.getLongitude() + "");
                        String address = amapLocation.getAddress();
                        String province = amapLocation.getProvince();
                        String city = amapLocation.getCity();
                        String district = amapLocation.getDistrict();
                        String street = amapLocation.getStreet();
                        double latitude = amapLocation.getLatitude();
                        double longitude = amapLocation.getLongitude();
                        HashMap<String, Object> hashMap = new HashMap<>();
                        hashMap.put("result", true);
                        if (!TextUtils.isEmpty(address) && !TextUtils.isEmpty(district) && address.contains(district)){
                            address = address.replaceFirst(district,"");
                        }
                        hashMap.put("address", address);
                        if (!TextUtils.isEmpty(province) && province.endsWith("市")) {
                            hashMap.put("province", province.substring(0, province.length() - 1));
                        } else {
                            hashMap.put("province", province);
                        }
                        //                    hashMap.put("province", province.endsWith("市") ? province.substring(0, province.length() - 1) : province);
                        hashMap.put("city", city);
                        if (TextUtils.isEmpty(district)) {
                            district = city;
                        }
                        hashMap.put("district", district);
                        hashMap.put("street", street);
                        hashMap.put("latitude", latitude);
                        hashMap.put("longitude", longitude);
                        ALog.e("oakkkmap","get location address111: " + province + " " + city  + " " + district + " " + street +" " + amapLocation.getCountry());
                        ALog.e("oakkkmap","get location address111: " + amapLocation.getLocationDetail());
                        ALog.e("oakkkmap","get location address111: " + amapLocation.getDescription());
                        ALog.e("oakkkmap","get location address111: " + amapLocation.getPoiName());

                        SharedPreferencesUtils.getInstance().save("localInfo",new Gson().toJson(hashMap));
                        promise.resolve(new Gson().toJson(hashMap));
                    }
                } else {
                    //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
//                    Toast.makeText(getApplicationContext(),"定位获取失败,请检查是否开启定位权限",Toast.LENGTH_SHORT).show();
                }
            }
        };
//设置定位回调监听
        mLocationClient.setLocationListener(mAMapLocationListener);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
//启动定位
        mLocationClient.startLocation();
    }

    public static void saveLocalInfo(Context context){
            AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
            mLocationOption.setOnceLocation(true);
            mLocationOption.setOnceLocationLatest(true);
            //初始化定位
            AMapLocationClient mLocationClient = new AMapLocationClient(MainApplication.getContext());
            AMapLocationListener mAMapLocationListener = new AMapLocationListener() {
                @Override
                public void onLocationChanged(AMapLocation amapLocation) {
                    if (amapLocation != null) {
                        if (amapLocation.getErrorCode() == 0) {
//可在其中解析amapLocation获取相应内容。
                            amapLocation.getLatitude();//获取纬度
                            amapLocation.getLongitude();//获取经度
                            amapLocation.getCity();//城市信息
                            JSONArray newArray = new JSONArray();
                            newArray.put(amapLocation.getCity());
                            newArray.put(amapLocation.getLatitude() + "");
                            newArray.put(amapLocation.getLongitude() + "");
                            String address = amapLocation.getAddress();
                            String province = amapLocation.getProvince();
                            String city = amapLocation.getCity();
                            String district = amapLocation.getDistrict();
                            String street = amapLocation.getStreet();
                            double latitude = amapLocation.getLatitude();
                            double longitude = amapLocation.getLongitude();
                            HashMap<String, Object> hashMap = new HashMap<>();
                            hashMap.put("result", true);
                            if (!TextUtils.isEmpty(address) && !TextUtils.isEmpty(district) && address.contains(district)){
                                address = address.replaceFirst(district,"");
                            }
                            hashMap.put("address", address);
                            if (!TextUtils.isEmpty(province) && province.endsWith("市")) {
                                hashMap.put("province", province.substring(0, province.length() - 1));
                            } else {
                                hashMap.put("province", province);
                            }
                            //                    hashMap.put("province", province.endsWith("市") ? province.substring(0, province.length() - 1) : province);
                            hashMap.put("city", city);
                            if (TextUtils.isEmpty(district)) {
                                district = city;
                            }
                            hashMap.put("district", district);
                            hashMap.put("street", street);
                            hashMap.put("latitude", latitude);
                            hashMap.put("longitude", longitude);
                            ALog.e("oakkkmap","get location address222: " + province + " " + city  + " " + district + " " + street);
                            SharedPreferencesUtils.getInstance().save("localInfo",new Gson().toJson(hashMap));
                        }
                    } else {
                        //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
//                    Toast.makeText(getApplicationContext(),"定位获取失败,请检查是否开启定位权限",Toast.LENGTH_SHORT).show();
                    }
                }
            };
//设置定位回调监听
            mLocationClient.setLocationListener(mAMapLocationListener);
            //给定位客户端对象设置定位参数
            mLocationClient.setLocationOption(mLocationOption);
//启动定位
            mLocationClient.startLocation();

    }

    public static String getLocalString(){
        return SharedPreferencesUtils.getInstance().getString("localInfo","");
    }

}
